package com.yf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.dto.Page;
import com.yf.entity.Subject;
import com.yf.entity.SubjectType;
import com.yf.mapper.SubjectMapper;
import com.yf.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SubjectServiceImpl implements SubjectService {

    @Autowired
    private SubjectMapper subjectMapper;

    @Override
    public void addSubject(Subject subject) {
        subjectMapper.addSubject(subject);
    }

    @Override
    public void deleteSubject(Integer subjectId) {
        subjectMapper.deleteSubject(subjectId);
    }

    @Override
    public void updateSubject(Subject subject) {
        subjectMapper.updateSubject(subject);
    }

    @Override
    public Subject getSubjectBySubjectId(Integer subjectId) {

        return subjectMapper.getSubjectBySubjectId(subjectId);
    }

    @Override
    public Page getSubjectPage(Integer pageNo, Integer pageSize) {
        Page page = new Page();

        PageHelper.startPage(pageNo,pageSize);
        List<Subject> subjectList = subjectMapper.getSubjectList();
        PageInfo<Subject> info = new PageInfo<>(subjectList);

        page.setPageNo(info.getPageNum());
        page.setPageSize(info.getPageSize());
        page.setPageCount(info.getPages());
        page.setCount((int) info.getTotal());
        page.setHasNext(info.isHasNextPage());
        page.setHasPre(info.isHasPreviousPage());
        page.setData(info.getList());

        return page;
    }
}
